package com.gwcm.controller;

import com.alipay.api.AlipayApiException;
import com.gwcm.common.annotation.UserId;
import com.gwcm.model.enums.PaymentTypeEnum;
import com.gwcm.service.IPayService;
import com.yhz.commonutil.common.BaseResponse;
import com.yhz.commonutil.common.ResultUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 支付控制器
 * @author yanhuanzhan
 * @date 2023/3/3 - 16:37
 */
@Api(tags = "支付功能")
@RestController
@RequestMapping("/pay")
public class PayController {

    @Resource
    private IPayService payService;
    @ApiOperation("支付宝网站支付下单")
    @PostMapping("/preOrder/alipay-pc/{orderNo}")
    public BaseResponse alipayPcPreOrder(@PathVariable("orderNo")String orderNo) {
        String formStr=payService.preOrder(orderNo);
        return ResultUtils.success(formStr);
    }
    @ApiOperation("支付宝支付回调接口")
    @PostMapping("/alipay/pay-notify")
    public String alipayAcceptNotify(@RequestParam Map<String, String> params) throws AlipayApiException {
        String result=payService.aliPayAcceptNotify(params);
        return result;
    }

    @ApiOperation("创建订单")
    @GetMapping("/create/{cardId}")
    public BaseResponse<String> createOrder(@PathVariable("cardId") Long cardId,@UserId Long userId) {
        String orderNo = payService.createOrder(cardId, userId, PaymentTypeEnum.ALI_PAY);
        return ResultUtils.success(orderNo);
    }
}
